﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;
    using WMS6.Web.UserControls;

    public class UserList : BasePage
    {
        protected Button btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F1022;
        protected GridView gvUser;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblRole;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2UserList(UserMstQueryEntity userQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(userQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = BaseInfoServiceClientProxy.GetInstance().GetInfoByQueryEntity(userQuery, "USER_MST");
            base.BindData2GridView(this.gvUser, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvUser, "USER_ID");
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1200_013");
                }
                else
                {
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(selectedID, "USER_MST") + base.GetGlobalMsgResource("F1200_010");
                    UserMstQueryEntity userMstInfo = this.GetUserQueryEntity();
                    this.BindData2UserList(userMstInfo);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1200_011") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvUser, "USER_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1200_013");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1200_014");
            }
            else
            {
                base.Response.Redirect(string.Format("User.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            UserMstQueryEntity userQuery = this.GetUserQueryEntity();
            userQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = userQuery.CurrentPage;
            userQuery.CurrentPage = "0";
            this.gvUser.Columns[1].Visible = false;
            this.gvUser.AllowSorting = false;
            this.BindData2UserList(userQuery);
            this.gvUser.ShowFooter = false;
            this.hdnActionID.Value = "";
            userQuery.IsGetAll = false;
            userQuery.CurrentPage = currentPageIndex;
            this.SetUserMstQueryEntity(userQuery);
            base.Export2Excel("User.xls", this.gvUser);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            UserMstQueryEntity userQuery = this.GetUserQueryEntity();
            userQuery.CurrentPage = "0";
            this.BindData2UserList(userQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            UserMstQueryEntity userQuery = this.GetUserQueryEntity();
            userQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2UserList(userQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            UserMstQueryEntity userMstQuery = this.GetUserQueryEntity();
            userMstQuery.UserID = "";
            userMstQuery.UserName = "";
            userMstQuery.City = "";
            userMstQuery.State = "";
            userMstQuery.Mobile = "";
            userMstQuery.Department = "";
            userMstQuery.Zip = "";
            userMstQuery.Country = "";
            userMstQuery.Phone = "";
            userMstQuery.Fax = "";
            userMstQuery.Email = "";
            this.BindData2UserList(userMstQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            UserMstQueryEntity userQuery = this.GetUserQueryEntity();
            if (int.Parse(userQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                userQuery.CurrentPage = (int.Parse(userQuery.CurrentPage) + 1).ToString();
                this.BindData2UserList(userQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            UserMstQueryEntity userQuery = this.GetUserQueryEntity();
            if (int.Parse(userQuery.CurrentPage) > 0)
            {
                userQuery.CurrentPage = (int.Parse(userQuery.CurrentPage) - 1).ToString();
                this.BindData2UserList(userQuery);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("UserQuery.aspx");
        }

        private UserMstQueryEntity GetUserQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as UserMstQueryEntity);
            }
            return (base.PageQueryEntitySession as UserMstQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                UserMstQueryEntity userQuery = this.GetUserQueryEntity();
                userQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2UserList(userQuery);
            }
        }

        protected void gvUser_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F1022", this.gvUser);
        }

        protected void gvUser_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvUser_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string strValue = base.UrlReplace(this.gvUser.DataKeys[e.NewSelectedIndex]["USER_ID"].ToString());
            if (!string.IsNullOrEmpty(strValue))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + strValue + "';  window.close();</script>", false);
            }
        }

        protected void gvUser_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                UserMstQueryEntity userMstQuery = this.GetUserQueryEntity();
                userMstQuery.SortField = e.SortExpression;
                if (userMstQuery.SortDirection == "ASC")
                {
                    userMstQuery.SortDirection = "DESC";
                }
                else if (userMstQuery.SortDirection == "DESC")
                {
                    userMstQuery.SortDirection = "ASC";
                }
                this.BindData2UserList(userMstQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            UserMstQueryEntity userMstQuery = new UserMstQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    userMstQuery.UserID = this.PreviousPage.UserId;
                    userMstQuery.UserName = this.PreviousPage.UserName;
                    userMstQuery.City = this.PreviousPage.City;
                    userMstQuery.Department = this.PreviousPage.Depatrment;
                    userMstQuery.Email = this.PreviousPage.Email;
                    userMstQuery.Fax = this.PreviousPage.Fax;
                    userMstQuery.State = this.PreviousPage.State;
                    userMstQuery.Zip = this.PreviousPage.Zip;
                    userMstQuery.Mobile = this.PreviousPage.Mobile;
                    userMstQuery.Country = this.PreviousPage.Country;
                    userMstQuery.Phone = this.PreviousPage.Phone;
                    userMstQuery.IsWhUser = this.PreviousPage.IsWhUser;
                }
                else
                {
                    userMstQuery.UserID = "";
                    userMstQuery.UserName = "";
                    userMstQuery.City = "";
                    userMstQuery.Department = "";
                    userMstQuery.Email = "";
                    userMstQuery.Fax = "";
                    userMstQuery.State = "";
                    userMstQuery.Zip = "";
                    userMstQuery.Mobile = "";
                    userMstQuery.Country = "";
                    userMstQuery.Phone = "";
                    userMstQuery.IsWhUser = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            userMstQuery.IsGetAll = false;
            userMstQuery.CurrentPage = "0";
            userMstQuery.PageSize = this.gvUser.PageSize.ToString();
            userMstQuery.SortField = "ROWID";
            userMstQuery.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvUser.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"];
                this.gvUser.Columns[1].Visible = false;
                this.gvUser.Columns[2].Visible = true;
                this.btnAction.Visible = false;
                this.btnDelete.Visible = false;
                this.btnEdit.Visible = false;
                this.btnExport.Visible = false;
                this.btnList.Visible = false;
                this.btnNew.Visible = false;
                this.btnReSearch.Visible = false;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetUserMstQueryEntity(userMstQuery);
            this.BindData2UserList(userMstQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetUserMstQueryEntity(UserMstQueryEntity userQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = userQuery;
            }
            else
            {
                base.PageQueryEntitySession = userQuery;
            }
        }

        public UserQuery PreviousPage
        {
            get
            {
                return (UserQuery) base.PreviousPage;
            }
        }
    }
}

